package com.bz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bz.dao.PreojectAreaDao;
import com.bz.dao.PreojectDao;
import com.bz.entity.PreojectAreaEntity;
import com.bz.entity.PreojectEntity;
import com.bz.model.ProjectDetailDto;
import com.bz.model.ProjectDto;
import com.bz.service.IProjectService;

@Service
public class PreojectServiceImpl implements IProjectService {

    @Autowired
    private PreojectDao     preojectDao;

    @Autowired
    private PreojectAreaDao preojectAreaDao;

    @Override
    public List<ProjectDto> list() {
        LambdaQueryWrapper<PreojectAreaEntity> queryarea = new LambdaQueryWrapper<>();
        queryarea.orderByAsc(PreojectAreaEntity::getName);
        List<PreojectAreaEntity> areas = preojectAreaDao.selectList(queryarea);
        if (CollectionUtils.isEmpty(areas)) {
            return null;
        }
        LambdaQueryWrapper<PreojectEntity> query = new LambdaQueryWrapper<>();
        query.orderByDesc(PreojectEntity::getSort).orderByDesc(PreojectEntity::getUpdateDate);
        List<PreojectEntity> datas = preojectDao.selectList(query);

        List<ProjectDto> list = new ArrayList<>();
        for (PreojectAreaEntity area : areas) {
            ProjectDto project = new ProjectDto();
            project.setAreaName(area.getName());
            if (!CollectionUtils.isEmpty(datas)) {
                project.setDatas(datas.stream().filter(it -> {
                    return area.getId() == it.getAreaId();
                }).map(it -> {
                    ProjectDetailDto d = new ProjectDetailDto();
                    d.setImg(it.getImg());
                    d.setName(it.getName());
                    return d;
                }).collect(Collectors.toList()));
            }
            list.add(project);
        }
        return list;
    }

}
